﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsmShaderConverter.AsmVertexShader
{
    class VertexShaderInstructions
    {

        public static string AsmFunctionMAD(
            VertexShaderDataTypes.Register firstValue, // Output
            VertexShaderDataTypes.Register secondValue, VertexShaderDataTypes.InOut ioSecondValue,// Vector
            VertexShaderDataTypes.Register thirdValue, VertexShaderDataTypes.InOut ioThirdValue, // Scalar for multiply
            VertexShaderDataTypes.Register fourthValue, VertexShaderDataTypes.InOut ioFourthValue) // Scalar for add
        {
            float multiplyValue = thirdValue.GetValue(ioThirdValue);
            float addValue = fourthValue.GetValue(ioFourthValue);

            if (secondValue.isUsed[0]) firstValue.X = secondValue.X * multiplyValue + addValue;
            if (secondValue.isUsed[1]) firstValue.Y = secondValue.Y * multiplyValue + addValue;
            if (secondValue.isUsed[2]) firstValue.Z = secondValue.Z * multiplyValue + addValue;
            if (secondValue.isUsed[3]) firstValue.W = secondValue.W * multiplyValue + addValue;

            string output = firstValue.VariableName + GetInOutPArameter(ioSecondValue);
            string calc = secondValue.VariableName + GetInOutPArameter(ioSecondValue) + " * "
                + thirdValue.VariableName + GetInOutPArameter(ioThirdValue) + " + "
                + fourthValue.VariableName + GetInOutPArameter(ioFourthValue);

            return output + " = " + calc;
        }

        private static string GetInOutPArameter(VertexShaderDataTypes.InOut value)
        {
            string output = ".";
            if ((value & VertexShaderDataTypes.InOut.X) == VertexShaderDataTypes.InOut.X) output += "X";
            if ((value & VertexShaderDataTypes.InOut.Y) == VertexShaderDataTypes.InOut.Y) output += "Y";
            if ((value & VertexShaderDataTypes.InOut.Z) == VertexShaderDataTypes.InOut.Z) output += "Z";
            if ((value & VertexShaderDataTypes.InOut.W) == VertexShaderDataTypes.InOut.W) output += "W";
            return output;
        }

    }
}
